using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using CKS.Core.Framework;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 底板铆接组装Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IDcBottomplaterivetingassembleService), ServiceLifetime = LifeTime.Transient)]
    public class DcBottomplaterivetingassembleService : BaseService<DcBottomplaterivetingassemble>, IDcBottomplaterivetingassembleService
    {
        /// <summary>
        /// 查询底板铆接组装列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<DcBottomplaterivetingassembleDto> GetList(DcBottomplaterivetingassembleQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.AssembleTime)
                .ToPage<DcBottomplaterivetingassemble, DcBottomplaterivetingassembleDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DcBottomplaterivetingassemble GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加底板铆接组装
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DcBottomplaterivetingassemble AddDcBottomplaterivetingassemble(DcBottomplaterivetingassemble model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改底板铆接组装
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateDcBottomplaterivetingassemble(DcBottomplaterivetingassemble model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<DcBottomplaterivetingassemble> QueryExp(DcBottomplaterivetingassembleQueryDto parm)
        {
            var predicate = Expressionable.Create<DcBottomplaterivetingassemble>();
            predicate.AndIF(parm.BeginTime != DateTime.MinValue && parm.BeginTime != null, u => u.AssembleTime >= parm.BeginTime.ToBeginTime());
            predicate.AndIF(parm.EndTime != DateTime.MinValue && parm.EndTime != null, u => u.AssembleTime <= parm.EndTime.ToEndTime());
            predicate.AndIF(!parm.BottomPlateCode.IsEmpty(), u => u.BottomPlateCode == parm.BottomPlateCode);
            return predicate;
        }
    }
}